package com.lzw.controller.dormitoryAdmin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.AbsenceRecord;
import com.lzw.service.dormitoryAdmin.AbsenceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/absence")
@CrossOrigin(origins = "http://localhost:7070")
public class AbsenceRecordController {

    @Autowired
    private AbsenceRecordService absenceRecordService;

    //分页查询缺勤记录
    @GetMapping("/list")
    public PageInfo<AbsenceRecord> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate absenceDate,
            String student){
        PageHelper.startPage(pageNum,pageSize);
        List<AbsenceRecord> list = absenceRecordService.list(pageNum,pageSize,absenceDate,student);
        return new PageInfo<>(list);
    }

    //批量删除缺勤记录
    @Log
    @DeleteMapping("/delete/{ids}")
    public String delete(@PathVariable List<Integer> ids){
        absenceRecordService.delete(ids);
        return "删除成功";
    }

    //添加缺勤记录
    @Log
    @PostMapping("/add")
    public String add(@RequestBody AbsenceRecord absenceRecord){
        absenceRecordService.add(absenceRecord);
        return "添加成功";
    }
}
